﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using MEF_Test.Bricks.Wrappers;
using Microsoft.Practices.Prism.Commands;

namespace MEF_Test.Bricks.Behaviors
{
    /// <summary>
    /// This class makes it possible to attach a command to an object's event, even other than <see cref="ButtonBase.Click"/>. for <see cref="FrameworkElement"/>s which don't inherit from <see cref="Control"/> and therefore, cannot leverage the mechanism from <see cref="CommandBehaviorBase{T}"/>.
    /// </summary>
    public class MydraMediaUriElementEventsToCommandsBehavior : DependencyObject
    {
        public static ICommand GetSeekToPositionCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(SeekToPositionCommandProperty);
        }

        public static void SetSeekToPositionCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(SeekToPositionCommandProperty, value);
        }

        // Using a DependencyProperty as the backing store for SeekToPositionCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SeekToPositionCommandProperty =
            DependencyProperty.RegisterAttached("SeekToPositionCommand", typeof(ICommand), typeof(MydraMediaUriElementEventsToCommandsBehavior), new UIPropertyMetadata(null, SeekToPositionCommandChanged));


        private static void SeekToPositionCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = (MydraMediaUriElement)d;
            
            // We never unregister : Memory leak ?            
            element.MediaPositionSeek += (o, args) => GetSeekToPositionCommand(element).Execute(args.NewPosition);            
        }


        public static ICommand GetBoundCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(BoundCommandProperty);
        }

        public static void SetBoundCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(BoundCommandProperty, value);
        }

        // Using a DependencyProperty as the backing store for MediaPositionChanged.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoundCommandProperty =
            DependencyProperty.RegisterAttached("BoundCommand", typeof(ICommand), typeof(MydraMediaUriElementEventsToCommandsBehavior), new UIPropertyMetadata(null, CommandChanged));

        private static void CommandChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
        {
           var mydraMediaUriElement = element as MydraMediaUriElement;            
            if (mydraMediaUriElement != null)
            {
                var command = ((ICommand)e.NewValue);
                mydraMediaUriElement.MediaPositionChanged += (o, ea) => command.Execute(mydraMediaUriElement.MediaPosition);
            }
        }
    }
}
